// import {reqGetCode, reqRegister, reqLogin, reqLogout, reqGetUserInfo, reqGetUserAddress} from '@/api/index'
import { postLogin, getConfig } from "@/api/index";
const state = {
    user: {},
    meeting: {},
    addresses: []
}
const mutations = {
    SETLOGINUSER(state, payload) {
        localStorage.setItem('token', payload.access_token)
    },
    SETUSERINFO(state, payload) {
        state.user = payload
        localStorage.setItem('userinfo', JSON.stringify(payload))
    },
    CLEARUSERINFO(state) {
        state.user = {},
        localStorage.removeItem('userinfo')
        localStorage.removeItem('token')
    },
    setMeeting(state, payload) {
        state.meeting = payload
    },
    SETUSERADDRESSLIST(state, payload) {
        state.addresses = payload
    }
}
const actions = {
    getCodeAction({commit}, phone) {
        // return reqGetCode(phone).then(response => {
        //     if (response.code != 200) {
        //         return Promise.reject(new Error(response.message || '请求失败'))
        //     }
        // }, error => {
        //     return Promise.reject(new Error('请求异常'))
        // })
    },
    registerAction({commit}, {phone, password, code}) {
        // return reqRegister(phone, password, code).then(response => {
        //     if (response.code != 200) {
        //         return Promise.reject(new Error(response.message || '请求失败'))
        //     }
        // }, error => {
        //     return Promise.reject(new Error('请求异常'))
        // })
    },
    loginAction({commit}, params) {
        return postLogin(params).then(response => {
            if (response.code == 200) {
                commit('SETLOGINUSER', response.data)
            } else {
                return Promise.reject(new Error(response.message || '请求失败'))
            }
        }, error => {
            return Promise.reject(new Error('请求异常'))
        })
    },
    getConfigAction({commit}, params) {
        return getConfig(params).then(response => {
            if (response.code == 200) {
                commit('setMeeting', response.data.meeting)
                return Promise.resolve(response)
            } else {
                return Promise.reject(new Error(response.message || '请求失败'))
            }
        }, error => {
            return Promise.reject(new Error('请求异常'))
        })
    },
    logoutAction({commit}) {
        // return reqLogout().then(response => {
        //     if (response.code == 200) {
        //         commit('CLEARUSERINFO')
        //     } else {
        //         return Promise.reject(new Error(response.message || '请求失败'))
        //     }
        // }, error => {
        //     return Promise.reject(new Error('请求异常'))
        // })
    },
    getUserInfoAction({commit}) {
        // return reqGetUserInfo().then(response => {
        //     // console.log(response)
        //     if (response.code == 200) {
        //         commit('SETUSERINFO', response.data)
        //     } else {
        //         return Promise.reject(new Error(response.message || '请求失败'))
        //     }
        // }, error => {
        //     return Promise.reject(new Error('请求异常'))
        // })
    },
    // 获取用户地址
    getUserAddressListAction({commit}) {
        // reqGetUserAddress().then(response => {
        //     if (response.code == 200) {
        //         commit('SETUSERADDRESSLIST', response.data)
        //     }
        // })
    }
}
const getters = {}
export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}